﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        private Graphics graphics;
     
        private double th1 = 30 * Math.PI / 180;
        private double th2 = 20 * Math.PI / 180;
        private double per1 = 0.6;
        private double per2 = 0.7;
        private int n = 10;
        private double leng = 100;

        public Form1()
        {
            InitializeComponent();
            graphics = panel.CreateGraphics();
            graphics.Clear(Color.White);
            BackColor = Color.Gray;

           
        }
        private void DrawCayleyTree(int n, double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);
            DrawLine(x0, y0, x1, y1);

            DrawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            DrawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }

        private void DrawLine(double x0, double y0, double x1, double y1)
        {
            Pen pen = Pens.Blue;
            switch (pentext.Text)
            {
                case "blue": pen = Pens.Blue; break;
                case "black": pen = Pens.Black; break;
                case "red": pen = Pens.Red; break;
            }
            graphics.DrawLine(
                    pen, (int)x0, (int)y0, (int)x1, (int)y1);
        }


        private void draw_Click(object sender, EventArgs e)
        {
            try
            {
                n = int.Parse(nTextBox.Text);
                leng = double.Parse(lengTextBox.Text);
                per1 = double.Parse(per1TextBox.Text);
                per2 = double.Parse(per2TextBox.Text);
                th1 = double.Parse(th1TextBox.Text) * Math.PI / 180;
                th2 = double.Parse(th2TextBox.Text) * Math.PI / 180;

            }
            catch (Exception)
            {
                MessageBox.Show("请检查输入数据格式是否正确！");
            }
            graphics.Clear(Color.White);
            DrawCayleyTree(n, panel.Width / 2, panel.Height - 10, leng, -Math.PI / 2);
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                n = int.Parse(nTextBox.Text);
                leng = double.Parse(lengTextBox.Text);
                per1 = double.Parse(per1TextBox.Text);
                per2 = double.Parse(per2TextBox.Text);
                th1 = double.Parse(th1TextBox.Text) * Math.PI / 180;
                th2 = double.Parse(th2TextBox.Text) * Math.PI / 180;

                graphics.Clear(Color.White);
                DrawCayleyTree(n, panel.Width / 2, panel.Height - 10, leng, -Math.PI / 2);
            }
            catch (Exception)
            {
                MessageBox.Show("请检查输入数据格式是否正确！");
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void pen_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void clear_Click(object sender, EventArgs e)
        {
            graphics.Clear(Color.White);
        }
     

        private void per2text_TextChanged(object sender, EventArgs e)
        {

        }

        private void per1TextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

